package graduate.cslg.com.universityshopping.bbs;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.List;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.adapter.BBSItemAdapter;
import graduate.cslg.com.universityshopping.entity.PostListBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class BBSActivity extends AppCompatActivity {

    private RecyclerView bbsRecyclerView;
    private List<PostListBean> mPostListBeans;
    private BBSItemAdapter mBBSItemAdapter;
    private Toolbar mToolbar;
    private final static int REQUEST_CODE = 1; // 返回的结果码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bbs);
        bbsRecyclerView = findViewById(R.id.bbs_recycler);
        mToolbar = findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        bbsRecyclerView.setLayoutManager(layoutManager);
        getBbsInfo();

    }

    private void getBbsInfo() {
        RetrofitDevelop.getTestService()
                .getBBSTopic()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PostListBean>>() {
                    @Override
                    public void onCompleted() {
                        mBBSItemAdapter = new BBSItemAdapter(mPostListBeans);
                        bbsRecyclerView.setAdapter(mBBSItemAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<PostListBean> beanList) {
                        mPostListBeans = beanList;
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_bbs, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.edit_bbs:
                Intent intent = new Intent(this, BBSEditActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                Toast.makeText(this, "发布成功！", Toast.LENGTH_SHORT).show();
                getBbsInfo();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBbsInfo();
    }
}
